package com.aaa.medicine.controller;


import com.aaa.entity.Medicines;
import com.aaa.medicine.service.IMedicinesService;
import com.aaa.medicine.vo.MedicinesVo;
import com.aaa.vo.Result;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("medicine/Medicines")
public class MedicinesController {
    @Autowired
    private IMedicinesService iMedicinesService;
    /**
     * 查询全部信息
     */
    @GetMapping("getAll")
    public Result getAll(Page page , Medicines medicines){
        IPage<Medicines> all = iMedicinesService.getAll(page,medicines.getMedicinesName(),medicines.getKeywords(),medicines.getStatus(),medicines.getMedicinesType(),medicines.getPrescriptionType(),medicines.getProducterId());
        return new Result(200,"查询成功",all);
    }
    /**
     * 根据id删除
     */
    @DeleteMapping("delById/{medicinesId}")
    public Result delById(@PathVariable Integer medicinesId){
        return new Result(iMedicinesService.removeById(medicinesId));
    }
    /**
     * 添加
     */
    @PostMapping("addStockProducerMapper")
    public Result addStockProducerMapper(@RequestBody Medicines medicines){
        return new Result(iMedicinesService.saveOrUpdate(medicines));
    }
    /**
     *批量删除
     */
    @PostMapping("delByIds")
    public Result delById(@RequestBody List<Medicines> medicines){
        medicines.forEach((Medicines medicines1)->{
            boolean b = iMedicinesService.removeById(medicines1.getMedicinesId());
        });
        return new Result(200,"",true);
    }
    @GetMapping("getAll2")
    public Result<List<Medicines>> getAll2(){
        List<Medicines> list = iMedicinesService.list();
        return new Result(list);
    }
    @PostMapping("getAll3")
    public Result getAll3(@RequestBody MedicinesVo medicines){
        IPage<Medicines> objectPage = new Page<>(medicines.getCurrent(),medicines.getSize());
        QueryWrapper<Medicines> wrapper = new QueryWrapper();
        if (StringUtils.isNotBlank(medicines.getKeywords())){
            wrapper.like("keywords",medicines.getKeywords());
        }
        if (StringUtils.isNotBlank(medicines.getMedicinesType())){
            wrapper.like("medicines_type",medicines.getMedicinesType());
        }
        if (StringUtils.isNotBlank(medicines.getPrescriptionType())){
            wrapper.like("prescription_type",medicines.getPrescriptionType());
        }
        IPage<Medicines> page1 = iMedicinesService.page(objectPage,wrapper);
        return new Result(page1);
    }

    /**
     * 修改药品数量
     */
    @GetMapping("upSum/{itemId}")
    public Boolean upSum(@PathVariable String itemId){
        return iMedicinesService.selectList(itemId);
    }
}
